package com.chuku.controller;

import com.bean.*;
import com.chuku.service.ChuKuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author wxrob
 */
@Controller
@RequestMapping("chuku")
public class ChuKuController {

    @Autowired
    ChuKuService chuKuService;

    //进入banli.html
    @RequestMapping("chuKu.do")
    public String chuKu(HttpServletRequest request) {
        String chuKuId = UUID.randomUUID().toString().replace("-", "").substring(0, 11);
        System.out.println("出库ID:" + chuKuId);
        request.getSession().setAttribute("chuKuId", chuKuId);
        return "chuku/banli";
    }

    @RequestMapping("huoZhu/{shuRu}")
    @ResponseBody
    public List<WmsTCustomer> huoZhu(@PathVariable(value = "shuRu") String shuRu) {
        System.out.println(shuRu);
        /*shuRu=new String(shuRu.getBytes("ISO-8859-1"),"utf-8");
        System.out.println(shuRu);*/
        List<WmsTCustomer> customers = chuKuService.getCusMess(shuRu);
        System.out.println(customers);
        return customers;
    }

    @RequestMapping("selChuKu/{custID}")
    @ResponseBody
    public List<WmsTBillDetails> selChuKu(@PathVariable(value = "custID") String custID, HttpServletRequest request) {
        System.out.println(custID);
        List<WmsTBillDetails> details = chuKuService.selChuKu(custID);
        System.out.println("details" + details);
        return details;
    }

    @RequestMapping("inChuKu.do")
    public String inChuKu(WmsTBill wmsTBill,String customerID, HttpServletRequest request) {
        //从域中获取userId
        WmsTUser wmsTUser = (WmsTUser) request.getSession().getAttribute("load_user");
        String billTransactor = wmsTUser.getUserID();
        wmsTBill.setBillTransactor(billTransactor);
        System.out.println("userid" + billTransactor);
        //根据发生时间生成最后编辑时间
        wmsTBill.setBillEditTime(wmsTBill.getBillDate());
        System.out.println(wmsTBill);
        chuKuService.inChuKu(wmsTBill);


        //办理出库插入账单详情表wms_t_bill_details,表办理出库更改WmsTTray表
        System.out.println("customerID"+customerID);
        List<WmsTBillDetails> insert  = chuKuService.selChuKu(customerID);
        for (WmsTBillDetails ad:insert) {
            //修改需要插入Details里所需要的数据
            String detailsID = UUID.randomUUID().toString().replace("-", "").substring(0, 3);
            ad.setDetailsID(detailsID);
            ad.setBillID(wmsTBill.getBillID());
            ad.setDetailsEditTime(wmsTBill.getBillEditTime());
            chuKuService.inDetails(ad);
            System.out.println("WmsTBillDetails"+ad);
            chuKuService.update(ad.getTrayID());
        }

        request.setAttribute("buttonState",1);
        return "chuku/banli";
    }

    //进入queren.html
    @RequestMapping("queRen.do")
    public String queRen(HttpServletRequest request) {
        List<WmsTStore> stores = chuKuService.selectStore();
        request.getSession().setAttribute("stores", stores);
        return "chuku/queren";
    }

    @RequestMapping("selStores.do")
    public String selStores(String storeID, String pageNum, String pageRows, HttpServletRequest request) {
        System.out.println("前端获取过来的storeID:" + storeID);
        System.out.println("前端传过来的页码:" + pageNum + "前端传过来的每页大小:" + pageRows);
        //开始分页
        if (pageRows == null || pageRows.equals("")) {
            PageHelper.startPage(1, 15);
            //pageSize & pageNum 放域
            request.getSession().setAttribute("pageNum1", 1);
            request.getSession().setAttribute("pageRows1", 15);
        } else {
            PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageRows));
            //pageSize & pageNum 放域
            request.getSession().setAttribute("pageNum1", Integer.parseInt(pageNum));
            request.getSession().setAttribute("pageRows1", Integer.parseInt(pageRows));
        }

        List<Map<String, Object>> all = chuKuService.selStores(storeID);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(all);
        System.out.println("pageInfo" + pageInfo);
        request.getSession().setAttribute("pageInfo1", pageInfo);
        request.getSession().setAttribute("storeID", storeID);
        return "chuku/queren";
    }

    //进入chaxun.html
    @RequestMapping("entChaXun.do")
    public String entChaXun() {

        return "chuku/chaxun";
    }


    @RequestMapping("chaxun.do")
    public void chaxun(String start, String end, String custName, String billID, String pageNum, String pageRows, HttpServletRequest request) {
        System.out.println("start:" + start + ";end:" + end + ";custName:" + custName + ";billID:" + billID);
        request.getSession().setAttribute("start1", start);
        request.getSession().setAttribute("end1", end);
        request.getSession().setAttribute("custName1", custName);
        request.getSession().setAttribute("billID1", billID);
        System.out.println("前端传过来的页码:" + pageNum + "前端传过来的每页大小:" + pageRows);

        //开始分页
        if (pageRows == null || pageRows.equals("")) {
            PageHelper.startPage(1, 15);
            //pageSize & pageNum 放域
            request.getSession().setAttribute("pageNum2", 1);
            request.getSession().setAttribute("pageRows2", 15);
        } else {
            PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageRows));
            //pageSize & pageNum 放域
            request.getSession().setAttribute("pageNum2", Integer.parseInt(pageNum));
            request.getSession().setAttribute("pageRows2", Integer.parseInt(pageRows));
        }
        List<Map<String, Object>> all = chuKuService.bill(start, end, custName, billID);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(all);
        System.out.println("pageInfo" + pageInfo);
        request.getSession().setAttribute("pageInfo2", pageInfo);

    }

    //mingxichaxun.html
    @RequestMapping("entmingxichaxun.do")
    public String entmingxichaxun() {

        return "chuku/mingxichaxun";
    }

    @RequestMapping("mingxichaxun.do")
    public String mingxichaxun(String start, String end, String custName, String billID, String pageNum, String pageRows, HttpServletRequest request) {
        System.out.println("start:" + start + ";end:" + end + ";custName:" + custName + ";billID:" + billID);
        request.getSession().setAttribute("start2", start);
        request.getSession().setAttribute("end2", end);
        request.getSession().setAttribute("custName2", custName);
        request.getSession().setAttribute("billID2", billID);
        System.out.println("前端传过来的页码:" + pageNum + "前端传过来的每页大小:" + pageRows);
        //开始分页
        if (pageRows == null || pageRows.equals("")) {
            PageHelper.startPage(1, 15);
            //pageSize & pageNum 放域
            request.getSession().setAttribute("pageNum3", 1);
            request.getSession().setAttribute("pageRows3", 15);
        } else {
            PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageRows));
            //pageSize & pageNum 放域
            request.getSession().setAttribute("pageNum3", Integer.parseInt(pageNum));
            request.getSession().setAttribute("pageRows3", Integer.parseInt(pageRows));
        }
        List<Map<String, Object>> all = chuKuService.details(start, end, custName, billID);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(all);
        System.out.println("pageInfo" + pageInfo);
        request.getSession().setAttribute("pageInfo3", pageInfo);

        return "chuku/mingxichaxun";
    }
}
